package studio.raptor.ddal.core.log.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import studio.raptor.ddal.core.log.common.CommonMethodUtil;
import studio.raptor.ddal.core.log.common.LogClientConstant;
import studio.raptor.ddal.core.log.queue.IQueueHelper;
import studio.raptor.ddal.core.log.service.interfaces.ILogDealServ;

public class LogDealServImpl implements ILogDealServ {

	private static Logger logger = LoggerFactory.getLogger(LogDealServImpl.class);
	private IQueueHelper queueHelper;
	private int depth;

	public LogDealServImpl(){
		if(queueHelper==null){
			queueHelper= CommonMethodUtil.getQueueHelper();
		}
		depth= LogClientConstant.queueDepth;
	}

//	@Override
//	public void logDeal(SqlProcessLogData sqlLogData) {
//		try{
//			int deep = queueHelper.getDepth();
//			if(deep < depth){
//				logger.debug("LogDealServ 队列写入对象SqlProcessLogData:"+sqlLogData.getTraceId());
//				queueHelper.writeObj(sqlLogData);
//			}else{
//				logger.error("直接放弃：java queue depth:"+deep+" > "+depth);
//			}
//		}catch(Exception e){
//			logFile(sqlLogData);
//		}
//	}

	@Override
	public void logDeal(Object logObject, String logType) {
		try{
			int deep = queueHelper.getDepth(logType);
			if(deep < depth){
//				logger.debug("LogDealServImpl 本地队列写入对象 logObeject:"+logObject.toString());
				queueHelper.writeObj(logObject,logType);
			}else{
				logger.error("直接放弃：java queue depth:"+deep+" > "+depth);
			}
		}catch(Exception e){
			logFile(logObject);
		}
	}

	public void logFile(Object logObject){
		//TODO 写入文件
	}

	public IQueueHelper getQueueHelper() {
		return queueHelper;
	}

	public int getDepth() {
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	public void setQueueIHelper(IQueueHelper queueHelper) {
		this.queueHelper = queueHelper;
	}
}
